
import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
 

public class EditedImage {	//
	Dimension thumbnailDimensionBig = new Dimension(128, 128);
	
	String type;
	String name;
	
	Dimension resizedDimension;
	
		
	private BufferedImage resizedImage;
	
	boolean upload = false;
		
	public ImageIcon thumbnailImageBig = null;
	
	String path; 
	int rotation=0;
	
	EditedImage(BufferedImage img, Dimension resizedDimension, String type, String name, String path) {
		this.type = type;
		this.name = name;
		this.path = path;
		
		this.resizedDimension = resizedDimension;		
		this.resizedImage = this.resizeImage(img, this.thumbnailDimensionBig);
		
		this.applyChanges();
		}

	public BufferedImage getResizedImage() {
		try {
			BufferedImage img = ImageIO.read(new File(this.path));
			
			img = this.resizeImage(img, resizedDimension);
			
			if (this.rotation <0){				
				for (int i=this.rotation%4; i<0 ; i++) {
					img = this.rotateLeft(img);					
				}
			}
			else if (this.rotation >0)	{
				for (int i=0; i< this.rotation%4; i++) {					
					img = this.rotateRight(img);
				}
			}
				
			return img;
			
		} 
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (BufferedImage)null;
	}
	
	public int x;
	public int y;
	
	public BufferedImage rotateLeft(BufferedImage img) {
		return this.rotate(90, img);
		}
	
	public BufferedImage rotateRight(BufferedImage img) {
		return this.rotate(-90, img);
		}

	public BufferedImage rotateLeft() {
		this.rotation--;
		return this.rotate(90, this.resizedImage);
		}
	
	public BufferedImage rotateRight() {
		this.rotation++;
		return this.rotate(-90, this.resizedImage);
		}
	
	private BufferedImage rotate(int angle, BufferedImage img) {		
		int w = img.getWidth();
		int h = img.getHeight();
		
		 
		BufferedImage newImage = new BufferedImage(h, w, img.getType());
		
		Graphics2D g = newImage.createGraphics();
		if (angle >0)
			g.rotate(Math.toRadians(angle), h/2, h/2);
		else
			g.rotate(Math.toRadians(angle), w/2, w/2);
		g.drawImage(img, null, 0, 0);
		
		resizedImage = newImage;
		
		this.applyChanges();		
		return newImage;
		}
	
	private void applyChanges() {		
		this.resizedImage = this.resizeImage(this.resizedImage, this.thumbnailDimensionBig);
		this.thumbnailImageBig = new ImageIcon(this.resizedImage);
		}	
	
	private BufferedImage resizeImage(BufferedImage img, Dimension newD) {
		int w = img.getWidth();
		int h = img.getHeight();
		
		int newW = 0;
		int newH = 0;
		if ((w > newD.getWidth()) || (h>newD.getHeight())) {			
			Dimension fitD = this.getNewDim(w, h, newD);
			newW = fitD.width;
			newH = fitD.height;
		}
		else {
			newW = w;
			newH = h;
		}
		
		BufferedImage newImage = new BufferedImage(newW, newH, img.getType());
		Graphics2D g = newImage.createGraphics();
		g.setComposite(AlphaComposite.Src);

		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
		RenderingHints.VALUE_INTERPOLATION_BILINEAR);

		g.setRenderingHint(RenderingHints.KEY_RENDERING,
		RenderingHints.VALUE_RENDER_QUALITY);

		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		RenderingHints.VALUE_ANTIALIAS_ON);
		g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
		
		g.dispose();
		
		return newImage;
		}
	
	private Dimension getNewDim(int w, int h, Dimension fitDimension) {
		int newW = 0;
		int newH = 0;
		double imgRatio = ((double)w)/h;
		double holdRatio = fitDimension.getWidth()/fitDimension.getHeight();
		
		if (imgRatio+.1f > holdRatio) {
			newW = (int)(fitDimension.getWidth());
			newH = (int)(fitDimension.getWidth()/imgRatio);
			}	
		else {
			newW = (int)(fitDimension.getWidth()*imgRatio);
			newH = (int)(fitDimension.getWidth());
			}		
		
		Dimension newD = new Dimension(newW, newH);
		return newD;
		}
	
}
